package com.wsd.jing.generator;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.generator
 * @className: AutoGenerator
 * @description AutoGenerator代码自动生成类
 * @createDate 2022/11/11 11:36
 * @copyright 2022/11/11 11:36 www.tom.com Inc. All rights reserved.
 */
public class AutoGenerator {

    private final static String URL = "jdbc:mysql://localhost:3306/jing?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8";
    private final static String USERNAME = "root";
    private final static String PASSWORD = "root";
    private final static String ENTITY_PREFIX = "J";

    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {

        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                // 拓展属性，用于在模板中新增字段，这里主要为了添加注释中的项目名称
                .injectionConfig((scanner, builder) -> builder.customMap(Collections.singletonMap("project", scanner.apply("请输入项目名称？"))))
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？")).fileOverride().enableSwagger().dateType(DateType.TIME_PACK).commentDate("yyyy-MM-dd hh:mm"))
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .convertFileName(entityName -> {
                            String fileName;
                            if (entityName.startsWith(ENTITY_PREFIX)) {
                                fileName = entityName.substring(1);
                            } else {
                                fileName = entityName;
                            }
                            return fileName + "Controller";
                        })
                        .entityBuilder().enableLombok().enableRemoveIsPrefix().enableTableFieldAnnotation().formatFileName("%sEntity")
                        .convertFileName(entityName -> {
                            String fileName;
                            if (entityName.startsWith(ENTITY_PREFIX)) {
                                fileName = entityName.substring(1);
                            } else {
                                fileName = entityName;
                            }
                            return fileName;
                        })
                        .mapperBuilder().enableBaseColumnList().enableBaseResultMap().enableMapperAnnotation()
                        .convertMapperFileName(entityName -> {
                            String fileName;
                            if (entityName.startsWith(ENTITY_PREFIX)) {
                                fileName = entityName.substring(1);
                            } else {
                                fileName = entityName;
                            }
                            return fileName + "Mapper";
                        })
                        .convertXmlFileName(entityName -> {
                            String fileName;
                            if (entityName.startsWith(ENTITY_PREFIX)) {
                                fileName = entityName.substring(1);
                            } else {
                                fileName = entityName;
                            }
                            return fileName + "Mapper";
                        })
                        .serviceBuilder()
                        .convertServiceFileName(entityName -> {
                            String fileName;
                            if (entityName.startsWith(ENTITY_PREFIX)) {
                                fileName = entityName.substring(1);
                            } else {
                                fileName = entityName;
                            }
                            return "I" + fileName + "Service";
                        })
                        .convertServiceImplFileName(entityName -> {
                            String fileName;
                            if (entityName.startsWith(ENTITY_PREFIX)) {
                                fileName = entityName.substring(1);
                            } else {
                                fileName = entityName;
                            }
                            return fileName + "ServiceImpl";
                        })
                        .build())
                // 模板配置
                .templateConfig((scanner, builder) -> builder
                        .entity("/templates/generator/entity.java")
                        .service("/templates/generator/service.java")
                        .serviceImpl("/templates/generator/serviceImpl.java")
                        .mapper("/templates/generator/mapper.java")
                        .mapperXml("/templates/generator/mapper.xml")
                        .controller("/templates/generator/controller.java"))
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
